package com.happycat.utils;

import java.util.Random;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.happycat.SysContants;
import com.happycat.mobile.model.UserShortMessage;

public class SmsUtils {
	
	

	
	public static String sendMessage(UserShortMessage userShortMessage) throws ServerException, ClientException {

		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", SysContants.ACCESS_KEY_ID, SysContants.ACCESS_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(SysContants.ALIYUN_SMS_URL);
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");                
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", userShortMessage.getPhone());
        request.putQueryParameter("SignName", userShortMessage.getSignName());
        request.putQueryParameter("TemplateCode", userShortMessage.getTemplateCode());
        request.putQueryParameter("TemplateParam", "{\"code\":"+userShortMessage.getMessage()+"}");
        CommonResponse response = client.getCommonResponse(request);
        return  response.getData();

	}
	
	  public static void main(String[] args) {
	        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",SysContants. ACCESS_KEY_ID, SysContants.ACCESS_SECRET);
	        IAcsClient client = new DefaultAcsClient(profile);
	        //{TemplateCode=SMS_147900664, PhoneNumbers=13539447522, RegionId=cn-hangzhou, TemplateParam={"code":592566}, SignName=happyCat}
	        CommonRequest request = new CommonRequest();
	        request.setSysMethod(MethodType.POST);
	        request.setSysDomain("dysmsapi.aliyuncs.com");
	        request.setSysVersion("2017-05-25");
	        request.setSysAction("SendSms");
	        request.putQueryParameter("RegionId", "cn-hangzhou");
	        request.putQueryParameter("PhoneNumbers", "13539447522");
	        request.putQueryParameter("SignName", "happyCat");
	        request.putQueryParameter("TemplateCode", "SMS_147900662");
	        request.putQueryParameter("TemplateParam", "{\"code\":120987}");
	        try {
	            CommonResponse response = client.getCommonResponse(request);
	            System.out.println(response.getData());
	        } catch (ServerException e) {
	            e.printStackTrace();
	        } catch (ClientException e) {
	            e.printStackTrace();
	        }
	    }
	  
	  public static String getVerifyCode() {
		  return String.valueOf(new Random().nextInt(8999) + 1000);//生成短信验证码
	  }
		/*
		 * public static getSign() { String accessKeyId = "testId"; String accessSecret
		 * = "testSecret";
		 * 
		 * java.util.Map<String, String> paras = new java.util.HashMap<String,
		 * String>(); }
		 */
}
